﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Net;
using System.Net.Sockets;

namespace NetMoveClient
{
    public partial class Form1 : Form
    {
        NetClient nclient;
        SendData sd = new SendData();
        public int[] user_id = new int[3];

        public Form1()
        {
            InitializeComponent();
      
        }

        private void button1_Click(object sender, EventArgs e)
        {
            nclient = new NetClient();
            nclient.Start(textBox1.Text);
            if (nclient.Handle.Connected)
            {
                button1.Visible = false;
                textBox1.Visible = false;
                listBox1.Visible = false;
                pictureBox1.Visible = true;
                pictureBox2.Visible = false;
                pictureBox3.Visible = false;
                pictureBox4.Visible = false;
            //    label1.Visible = false;
                if (sd.bird == 0) pictureBox1.Image = Properties.Resources._1;
                if (sd.bird == 1) pictureBox1.Image = Properties.Resources._2;
                if (sd.bird == 2) pictureBox1.Image = Properties.Resources._3;
                if (sd.bird == 3) pictureBox1.Image = Properties.Resources._4;
            }
        }
        private void Refresh()
        { 
        
        }
        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (listBox1.SelectedIndex == 0)
            {
                sd.bird = 0;
                pictureBox1.Visible = true;
                pictureBox2.Visible = false;
                pictureBox3.Visible = false;
                pictureBox4.Visible = false;
            }
            if (listBox1.SelectedIndex == 1)
            {
                sd.bird = 1;
                pictureBox1.Visible = false;
                pictureBox2.Visible = true;
                pictureBox3.Visible = false;
                pictureBox4.Visible = false;
            }
            if (listBox1.SelectedIndex == 2)
            {
                sd.bird = 2;
                pictureBox1.Visible = false;
                pictureBox2.Visible = false;
                pictureBox3.Visible = true;
                pictureBox4.Visible = false;
            }
            if (listBox1.SelectedIndex == 3)
            {
                sd.bird = 3;
                pictureBox1.Visible = false;
                pictureBox2.Visible = false;
                pictureBox3.Visible = false;
                pictureBox4.Visible = true;
            }
        }
        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            if (nclient != null)
                if (nclient.Handle.Connected == true)
                {
                    sd.x = e.X;
                    sd.y = e.Y;
                    pictureBox1.Left = sd.x;
                    pictureBox1.Top = sd.y;
                }
        }
        private void timer1_Tick(object sender, EventArgs e)
        {
            if (nclient != null)
                if (nclient.Handle.Connected)
                {
                    nclient.Send(sd);
                    SendData recData;

                    while (nclient.Handle.Available > 0) 
                    {

                        nclient.Receive(out recData);
                        textBox2.Text += string.Format("ID: {0}", recData.id_player)+Environment.NewLine;
                        label1.Text = nclient.rbyte.ToString();
                        label2.Text = recData.id_player.ToString();
                        label3.Text = user_id[0].ToString();
                        label4.Text = user_id[1].ToString();
                        label5.Text = user_id[2].ToString();
                        label6.Text = string.Format("{0}",nclient.Handle.Available);
                        for (int i = 0; i < 3; i++)
                        {
                            if ((i<2) &&(user_id[i] == user_id[i + 1])) user_id[i + 1] = 0;
                            if (user_id[i] != recData.id_player)
                            {
                                if (user_id[i] == 0) user_id[i] = recData.id_player;
                            }
                            if (user_id[i] == recData.id_player)
                            {
                                if (i == 0)
                                {
                                    pictureBox2.Visible = true;
                                    pictureBox2.Left = recData.x;
                                    pictureBox2.Top = recData.y;
                                    if (recData.bird == 0)
                                        if (pictureBox2.Image != Properties.Resources._1)
                                            pictureBox2.Image = Properties.Resources._1;
                                    if (recData.bird == 1)
                                        if (pictureBox2.Image != Properties.Resources._2)
                                            pictureBox2.Image = Properties.Resources._2;

                                    if (recData.bird == 2)
                                        if (pictureBox2.Image != Properties.Resources._3)
                                            pictureBox2.Image = Properties.Resources._3;
                                    if (recData.bird == 3)
                                        if (pictureBox2.Image != Properties.Resources._4)
                                            pictureBox2.Image = Properties.Resources._4;
                                }
                                if (i == 1)
                                {
                                    pictureBox3.Visible = true;
                                    pictureBox3.Left = recData.x;
                                    pictureBox3.Top = recData.y;
                                    if (recData.bird == 0)
                                        if (pictureBox3.Image != Properties.Resources._1)
                                            pictureBox3.Image = Properties.Resources._1;
                                    if (recData.bird == 1)
                                        if (pictureBox3.Image != Properties.Resources._2)
                                            pictureBox3.Image = Properties.Resources._2;

                                    if (recData.bird == 2)
                                        if (pictureBox3.Image != Properties.Resources._3)
                                            pictureBox3.Image = Properties.Resources._3;
                                    if (recData.bird == 3)
                                        if (pictureBox3.Image != Properties.Resources._4)
                                            pictureBox3.Image = Properties.Resources._4;
                                }
                                if (i == 2)
                                {
                                    pictureBox4.Visible = true;
                                    pictureBox4.Left = recData.x;
                                    pictureBox4.Top = recData.y;
                                    if (recData.bird == 0)
                                        if (pictureBox4.Image != Properties.Resources._1)
                                            pictureBox4.Image = Properties.Resources._1;
                                    if (recData.bird == 1)
                                        if (pictureBox4.Image != Properties.Resources._2)
                                            pictureBox4.Image = Properties.Resources._2;

                                    if (recData.bird == 2)
                                        if (pictureBox4.Image != Properties.Resources._3)
                                            pictureBox4.Image = Properties.Resources._3;
                                    if (recData.bird == 3)
                                        if (pictureBox4.Image != Properties.Resources._4)
                                            pictureBox4.Image = Properties.Resources._4;
                                }
                            }
                        }
                    }
                }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void Form1_Load_1(object sender, EventArgs e)
        {

        }

        private void timer2_Tick(object sender, EventArgs e)
        {

        }
    }
}